import React, { useEffect, useState } from 'react';
import { useNavigate } from 'react-router-dom';

function User() {  
  const nav = useNavigate();
  const [isLogin, setIsLogin] = useState(false);

  useEffect(() => {
    const token = localStorage.getItem('token');
    if (token) {
      setIsLogin(true);
    } else {
      setIsLogin(false);
      nav('/login');
    }
  }, [nav]);  

  return <div>{isLogin ? '已登录' : '未登录'}</div>;  
}

export default User;